<?php

namespace App\Admin\Controllers;

use App\Models\GoodsCategory;
use App\Models\Shopwindow;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Env;
use App\Admin\Actions\Grid\GoodsOrder\CreateOrder;

class GoodsOrderCreateController extends AdminController
{

    protected $translation = 'good';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Shopwindow(), function (Grid $grid) {

            $grid->paginate(50);
            $grid->model()->whereHas(
                'goods',function($query){
                    $query->where('status',1)->whereHas('category', function ($query) {
                        $query->where('status', GoodsCategory::STATUS_YES);
                    });
                }
            )->with(['goods','user'])->where('shelves','=',1)->orderByDesc('id');

            $grid->model()->orderByDesc('id');

            //$grid->column('id');
            $grid->column('user.tiktokno',trans('user.fields.tiktokno'));
            $grid->column('goods.code',trans('good.fields.code'));
            $grid->column('goods.name',trans('good.fields.name'))->width(300);
            $grid->column('goods.price',trans('good.fields.price'))->display(function ($var) {
                return $var / 100;
            });
            $grid->column('goods.original_price',trans('good.fields.original_price'))->display(function ($var) {
                return $var / 100;
            });
            $grid->column('goods.profit',trans('good.fields.profit'))->display(function ($var) {
                return $var / 100;
            });


            $grid->quickSearch('user.tiktokno');
            $grid->filter(function (Grid\Filter $filter) {


            });
            $grid->batchActions(function (Grid\Tools\BatchActions $actions) {
                $actions->add(new CreateOrder());
                $actions->disableDelete();
            });


            $grid->disableDeleteButton();
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->disableDeleteButton();
            $grid->disableActions();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GoodsOrder(), function (Show $show) {
            $show->field('id');
            $show->field('order_sn');
            $show->field('user_id');
            $show->field('order_status');
            $show->field('pay_time');
            $show->field('delivery_time');
            $show->field('order_create_type');
            $show->field('complete_time');
            $show->field('remark');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GoodsOrder(), function (Form $form) {
            $form->display('id');
            $form->text('order_sn');
            $form->text('user_id');
            $form->text('order_status');
            $form->text('pay_time');
            $form->text('delivery_time');
            $form->text('order_create_type');
            $form->text('complete_time');
            $form->text('remark');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }


    public function generalCreate(){
        if(Admin::user()->isAdministrator()) {
            $agent_id = 0;
        }else{
            $agent_id = Admin::user()->id;
        }
        $data = [
            'agent_id' => $agent_id,
            'api_url'  => config('app.url')
        ];
        return view('Admin.generalCreate', $data);
    }

    public function autoCreate(){
        if(Admin::user()->isAdministrator()) {
            $agent_id = 0;
        }else{
            $agent_id = Admin::user()->id;
        }


        $data = [
            'agent_id' => $agent_id,
            'api_url'  => config('app.url')
        ];
        return view('Admin.autoCreate', $data);
    }



}
